﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Collections
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using Falcon.Core.Domain;

	public class PagedList<T, TK> : IPagedList<T, TK>
		where TK : class, ISpecification, new()
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PagedList&lt;T, TK&gt;"/> class.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <param name="items">The items.</param>
		/// <param name="total">The total.</param>
		public PagedList(TK specification, IList<T> items, int total)
		{
			this.Specification = specification;
			this.Items = items;
			this.Page = specification.PageOrDefault;
			this.Limit = specification.LimitOrDefault;
			this.Total = total;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="PagedList&lt;T, TK&gt;"/> class.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <param name="items">The items.</param>
		/// <param name="page">The page.</param>
		/// <param name="limit">The limit.</param>
		/// <param name="total">The total.</param>
		public PagedList(TK specification, IList<T> items, int page, int limit, int total)
		{
			if (items == null)
			{
				throw new ArgumentNullException("items");
			}

			if (page <= 0)
			{
				throw new ArgumentOutOfRangeException("page", "page must be greater then 0");
			}

			if (limit <= 0)
			{
				throw new ArgumentOutOfRangeException("limit", "limit must be greater then 0");
			}

			if (total < 0)
			{
				throw new ArgumentOutOfRangeException("total", "total must be greater then or equal to 0");
			}

			this.Specification = specification;
			this.Items = items;
			this.Page = page;
			this.Limit = limit;
			this.Total = total;
		}

		/// <summary>
		/// Gets or sets the specification.
		/// </summary>
		/// <value>The specification.</value>
		public TK Specification
		{
			get;
			set;
		}

		public IList<T> Items
		{
			get;
			private set;
		}

		public int Page
		{
			get;
			set;
		}

		public int Limit
		{
			get;
			set;
		}

		public int Total
		{
			get;
			set;
		}

		public int Pages
		{
			get
			{
				return (int)Math.Ceiling(((double)Total) / ((double)Limit));
			}
		}

		public int FirstIndex
		{
			get
			{
				return Limit * (Page - 1);
			}
		}

		public int LastIndex
		{
			get
			{
				return Math.Min(FirstIndex + (Limit - 1), Total - 1);
			}
		}

		public int StartIndex
		{
			get
			{
				return this.FirstIndex + 1;
			}
		}

		public int EndIndex
		{
			get
			{
				return this.LastIndex + 1;
			}
		}

		public int PrevPage
		{
			get
			{
				return Page - 1;
			}
		}

		public int NextPage
		{
			get
			{
				return Page + 1;
			}
		}

		public bool HasPrev
		{
			get
			{
				return (FirstIndex != 0);
			}
		}

		public bool HasNext
		{
			get
			{
				return (Total == -1 || (FirstIndex + Limit) < Total);
			}
		}

		/// <summary>
		/// Returns an enumerator that iterates through the collection.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
		/// </returns>
		public IEnumerator<T> GetEnumerator()
		{
			return this.Items.GetEnumerator();
		}

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
		/// </returns>
		IEnumerator IEnumerable.GetEnumerator()
		{
			return this.Items.GetEnumerator();
		}
	}
}